package com.wei.lolbox.dao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.wei.lolbox.model.discover.HeroPosition;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "HERO_POSITION".
*/
public class HeroPositionDao extends AbstractDao<HeroPosition, Long> {

    public static final String TABLENAME = "HERO_POSITION";

    /**
     * Properties of entity HeroPosition.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Position_in_cn = new Property(1, String.class, "position_in_cn", false, "POSITION_IN_CN");
        public final static Property Name = new Property(2, String.class, "name", false, "NAME");
    }

    private Query<HeroPosition> heroes_PositionsQuery;

    public HeroPositionDao(DaoConfig config) {
        super(config);
    }
    
    public HeroPositionDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"HERO_POSITION\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"POSITION_IN_CN\" TEXT," + // 1: position_in_cn
                "\"NAME\" TEXT NOT NULL );"); // 2: name
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"HERO_POSITION\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, HeroPosition entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String position_in_cn = entity.getPosition_in_cn();
        if (position_in_cn != null) {
            stmt.bindString(2, position_in_cn);
        }
        stmt.bindString(3, entity.getName());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, HeroPosition entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String position_in_cn = entity.getPosition_in_cn();
        if (position_in_cn != null) {
            stmt.bindString(2, position_in_cn);
        }
        stmt.bindString(3, entity.getName());
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public HeroPosition readEntity(Cursor cursor, int offset) {
        HeroPosition entity = new HeroPosition( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // position_in_cn
            cursor.getString(offset + 2) // name
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, HeroPosition entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setPosition_in_cn(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setName(cursor.getString(offset + 2));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(HeroPosition entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(HeroPosition entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(HeroPosition entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "positions" to-many relationship of Heroes. */
    public List<HeroPosition> _queryHeroes_Positions(String name) {
        synchronized (this) {
            if (heroes_PositionsQuery == null) {
                QueryBuilder<HeroPosition> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Name.eq(null));
                heroes_PositionsQuery = queryBuilder.build();
            }
        }
        Query<HeroPosition> query = heroes_PositionsQuery.forCurrentThread();
        query.setParameter(0, name);
        return query.list();
    }

}
